package cn.tedu.xuecha.domain.service;

import cn.tedu.xuecha.common.ex.ServiceException;
import cn.tedu.xuecha.common.pojo.Content;
import cn.tedu.xuecha.common.respone.AppCode;
import cn.tedu.xuecha.dao.ContentDAO;
import cn.tedu.xuecha.domain.repository.ContentRepository;
import cn.tedu.xuecha.protocol.param.ContentDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author coinyang
 * @date 2023/4/23 10:53
 */
@Service
public class ContentService {

    @Autowired
    ContentRepository contentRepository;
    @Autowired
    ContentDAO contentDAO;




    public void addContent(ContentDTO contentDTO) {
//        if(contentDTO ==null){
//            String message = "新增文章不能为空";
//            throw new ServiceException( AppCode.ERR_CONFLICT,message);
//        }
        Content content = new Content();
        BeanUtils.copyProperties(contentDTO,content);
        content.setId(2);
//        Content content = contentConverter.toContent(record);
        int rows = contentDAO.insert(content);
        if(rows == 0 ){
            String message = "文章新增失败";
            throw  new ServiceException(AppCode.ERR_INSERT,message);
        }
    }

    public List<Content> list(){
        return contentRepository.list();
    }
    public void updateContent(ContentDTO contentDTO,Integer id) {
//        if(contentDTO ==null){
//            String message = "文章不能为空";
//            throw new ServiceException( AppCode.ERR_CONFLICT,message);
//        }
        Content content = new Content();
        BeanUtils.copyProperties(contentDTO,content);
        content.setId(id);
        int rows = contentDAO.updateByPrimaryKeySelective(content);
        if(rows == 0 ){
            String message = "文章更新失败";
            throw  new ServiceException(AppCode.ERR_INSERT,message);
        }
    }
}
